home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 23 / CU Amiga - Super CD-ROM 23 (June 1998).iso / CUCD / Graphics / STIMP_noise / source / include / STIMP / pbm.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-02-14  |  4.0 KB  |  204 lines

  1.  
  2. // PBM: declarations and routines
  3. // Version 2.10, 11.Feb.98
  4. // (c) 1997/98 by Stefan Diener
  5.  
  6. #ifndef STIMP_PBM_INC
  7. #define STIMP_PBM_INC
  8.  
  9. #include <STIMP/misc.c>
  10.  
  11. struct PBM_Info
  12. {
  13.   unsigned int height;
  14.   unsigned int width;
  15.   unsigned char *Data;
  16. };
  17.  
  18. int ReadPBMFile(char *name, struct PBM_Info *info)
  19. // read a PBM-file
  20. {
  21.   FILE *datei;
  22.   char tempo[5];
  23.   int i, j, k, zeile;
  24.   unsigned char *src, zwisch=0;
  25.  
  26.   if (strcmp("-",name)==0) datei=stdin;   // use stdin
  27.   else
  28.   {
  29.     // really open a file
  30.     datei=fopen(name,"rb");
  31.     if (datei==0)
  32.     {
  33.       PrintMessage("File %s not found !", name);
  34.       return 1;
  35.     }
  36.   }
  37.  
  38.   // set buffer size
  39.   setvbuf(datei,0,_IOFBF,500);
  40.  
  41.   // check type
  42.   fgets(tempo, 4, datei);
  43.   correct(tempo);
  44.   if (strcmp(tempo,"P4")!=0)
  45.   {
  46.     PrintMessage("Unknown file type: %s !",name);
  47.     if (strcmp("-",name)!=0) fclose(datei);
  48.     return 1;
  49.   }
  50.  
  51.   // read dimensions
  52.   // skip comments !!!
  53.   ReadComment(datei);
  54.   fscanf(datei,"%i %i\n",&info->width,&info->height);
  55.  
  56.   // print it on the screen
  57.   PrintMessage("Reading %s (PBM, %ix%i)", name, info->width, info->height);
  58.  
  59.   // allocate memory for the picture
  60.   info->Data=NULL;
  61.   info->Data=(unsigned char *) malloc(info->height*info->width*sizeof(unsigned char));
  62.   if (info->Data==NULL)
  63.   {
  64.     PrintMessage("No memory for the picture data left !");
  65.     if (strcmp("-",name)!=0) fclose(datei);
  66.     return 1;
  67.   }
  68.  
  69.   // need an empty picture
  70.   src=info->Data;
  71.   for (i=0;i<info->width*info->height;i++) *src++=0;
  72.  
  73.   // initialize help variables
  74.   src=info->Data;
  75.   zeile=info->width;
  76.   if (zeile%8) zeile=zeile-(zeile%8)+8;
  77.   zeile=zeile/8;
  78.  
  79.   // read picture data
  80.   for (i=0; i<info->height; i++)
  81.   {
  82.     for (k=0; k<zeile; k++)
  83.     {
  84.       j=fgetc(datei);
  85.       if (j==EOF)
  86.       {
  87.         PrintMessage("Unexpected end of file !");
  88.         i=info->height;
  89.         k=zeile;
  90.       }
  91.       else
  92.       {
  93.         zwisch=(unsigned char) j;
  94.         for (j=7; j>=0; j--)
  95.         {
  96.           // Bit==0 => white, Bit==1 => black
  97.           if (k*8+j<info->width) src[i*info->width+k*8+j]=((zwisch%2)?0:255);
  98.           zwisch=zwisch>>1;
  99.         }
  100.       }
  101.     }
  102.   }
  103.  
  104.   // close file
  105.   if (strcmp("-",name)!=0) fclose(datei);
  106.  
  107.   // ok
  108.   return 0;
  109. }
  110.  
  111. int WritePBMFile(char *name, struct PBM_Info *info)
  112. // write a PBM-file
  113. {
  114.   FILE *datei;
  115.   int i, j, zeile;
  116.   unsigned char *dst, zwisch=0;
  117.  
  118.   if (strcmp("-",name)==0) datei=stdout;   // use stdout
  119.   else
  120.   {
  121.     // really open a file
  122.     datei=fopen(name,"wb");
  123.     if (datei==0)
  124.     {
  125.       PrintMessage("Could not open destination file %s !", name);
  126.       return 1;
  127.     }
  128.   }
  129.  
  130.   PrintMessage("Writing %s (PBM, %ix%i)", name, info->width, info->height);
  131.  
  132.   // set buffer size
  133.   setvbuf(datei,0,_IOFBF,500);
  134.  
  135.   // write file header
  136.   fprintf(datei,"P4\n");
  137.   fprintf(datei,"# written by: "OP_NAME" "VERSION" ("DATE"), author: "AUTHOR"\n");
  138.   fprintf(datei,"%i %i\n",info->width,info->height);
  139.  
  140.   // write picture data
  141.   dst=info->Data;
  142.   zeile=info->width;
  143.   if (zeile%8) zeile=zeile-(zeile%8)+8;
  144.  
  145.   for (i=0; i<info->height;i++)
  146.   {
  147.     for (j=0; j<zeile; j++)
  148.     {
  149.       zwisch=(zwisch<<1);
  150.  
  151.       // white => Bit=0, black => Bit=1
  152.       if (j<info->width) zwisch=zwisch+(dst[i*info->width+j]?0:1);
  153.  
  154.       if (j%8==7)
  155.       {
  156.         fputc(zwisch,datei);
  157.         zwisch=0;
  158.       }
  159.     }
  160.   }
  161.  
  162.   // flush buffer and close file
  163.   fflush(datei);
  164.   if (strcmp("-",name)!=0) fclose(datei);
  165.  
  166.   // ok
  167.   return 0;
  168. }
  169.  
  170. int CreatePBMArray(int y, int x, struct PBM_Info *info)
  171. // allocate memory for a PBM-picture
  172. {
  173.   int i;
  174.   unsigned char *dst;
  175.  
  176.   // save dimensions and maxval
  177.   info->width=x;
  178.   info->height=y;
  179.  
  180.   // allocate memory
  181.   info->Data=NULL;
  182.   info->Data=(unsigned char *) malloc(x*y*sizeof(unsigned char));
  183.   if (info->Data==NULL)
  184.   {
  185.     PrintMessage("No memory for picture data left !");
  186.     return 1;
  187.   }
  188.  
  189.   // need an empty picture
  190.   dst=info->Data;
  191.   for (i=0;i<info->width*info->height;i++) *dst++=0;
  192.  
  193.   // ok
  194.   return 0;
  195. }
  196.  
  197. void FreePBMArray(struct PBM_Info *info)
  198. // free the memory of a PBM-picture
  199. {
  200.   free((void *) info->Data);
  201. }
  202.  
  203. #endif
  204.